1.限制只能输入正数和小数保留小数点后两位
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1
2
3
4 $(function(){
5 $("#txtNum").keyup(function () {
6 $(this).val(ChangeNumValue($(this).val()));
7 });
8
9
10 });
11
12 function ChangeNumValue(tmpVal)
13 {
14 if (tmpVal) {
15 var tmpVal = tmpVal.replace(/[^\d\.]/g, '');
16 var reg = /^(0|([1-9]\d*))(\.\d{1,2})?$/; //正则验证保留 最多允许后输入两位小数
17 if (!reg.test(tmpVal)) {
18 tmpVal = tmpVal + "";
19 tmpVal = tmpVal.substring(0, tmpVal.indexOf(".") + 3);
20 var n = (tmpVal.split('.')).length - 1;
21 if (n > 1) {
22 tmpVal = tmpVal.substring(0, tmpVal.indexOf("."));
23 }
24 }
25 return tmpVal;
26 } else {
27 return "";
28 }
29 }
30
View Code
2.只能输入正整数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1
View Code
3.输入正数和小数(保留两位)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1
View Code
|